package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.TTcProductAlarmMapper;
import com.zhijian.medical.entity.po.TTcProductAlarm;
import com.zhijian.medical.entity.vo.request.TTcProductAlarmSearchVo;
import com.zhijian.medical.entity.vo.response.TTcProductAlarmVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.TTcProductAlarmService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author HCG
 * @version 1.0
 * @date 2021/9/2
 */
@Slf4j
@Service
public class TTcProductAlarmServiceImpl implements TTcProductAlarmService {

    @Autowired
    private TTcProductAlarmMapper mapper;

    @Override
    public TTcProductAlarm selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TTcProductAlarmVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(TTcProductAlarm tTcProductAlarm) {
        Date now = new Date();
        tTcProductAlarm.setCreateTime(now);
        tTcProductAlarm.setUpdateTime(now);
        return mapper.insert(tTcProductAlarm);
    }

    @Override
    public boolean update(TTcProductAlarm tTcProductAlarm) {
        Date now = new Date();
        tTcProductAlarm.setUpdateTime(now);
        return mapper.update(tTcProductAlarm);
    }

    @Override
    public Integer count(TTcProductAlarmSearchVo tTcProductAlarmSearchVo) {
        return mapper.count(tTcProductAlarmSearchVo);
    }

    @Override
    public List<TTcProductAlarmVo> selectList(TTcProductAlarmSearchVo tTcProductAlarmSearchVo) {
        return mapper.selectList(tTcProductAlarmSearchVo);
    }

    @Override
    public Page<TTcProductAlarmVo> selectPage(TTcProductAlarmSearchVo tTcProductAlarmSearchVo) {
        if (tTcProductAlarmSearchVo.getPageNum() < 1 || tTcProductAlarmSearchVo.getPageSize() < 1) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "分页参数不合法");
        }
        //先直接查总数
        int count = count(tTcProductAlarmSearchVo);
        //分页
        Page<TTcProductAlarmVo> page = new Page<>(true, count, tTcProductAlarmSearchVo.getPageNum(), tTcProductAlarmSearchVo.getPageSize());
        tTcProductAlarmSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TTcProductAlarmVo> responseVos = this.selectList(tTcProductAlarmSearchVo);
        page.setData(responseVos);
        return page;
    }
}
